package org.juggers.client.ui.component;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Iterator;

import org.juggers.client.JuggersMessages;
import org.juggers.client.controller.EventController;
import org.juggers.domain.Event;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Jug Event component
 * 
 * @author <a href="mailto:michel@breizhjug.org">Michel DAVID</a>
 */
public class EventPanel
    extends Composite
{

    private static final DateTimeFormat dateFormatter = DateTimeFormat.getFormat( "EEEE dd MMMM yyyy" );

    private static final DateTimeFormat timeFormatter = DateTimeFormat.getFormat( "HH:mm" );

    private Event event;

    private Image logoEvent;

    private Label title;

    private CombinedLabel summary;

    private CombinedLabel speakers;

    private CombinedLabel jug;

    private CombinedLabel date;

    private CombinedLabel time;

    private CombinedLabel place;

    private CombinedLabel seats;

    /**
     * Constructor
     * 
     * @param event Event
     * @param messages Localized messages
     */
    public EventPanel( final EventController eventController, JuggersMessages messages )
    {
        FlowPanel eventPanel = new FlowPanel();
        eventPanel.setStyleName( "event" );

        HTMLTable tableEvent = new FlexTable();
        tableEvent.setStyleName( "table-event" );
        eventPanel.add( tableEvent );

        logoEvent = new Image();
        logoEvent.setStyleName( "logo-event" );

        tableEvent.getCellFormatter().addStyleName( 0, 0, "logo-event" );
        tableEvent.setWidget( 0, 0, logoEvent );

        VerticalPanel eventContent = new VerticalPanel();

        title = new Label();
        eventContent.add( title );
        summary = new CombinedLabel( messages.description() + " : " );
        eventContent.add( summary );
        speakers = new CombinedLabel( messages.speakers() + " : " );
        eventContent.add( speakers );

        tableEvent.getCellFormatter().addStyleName( 0, 1, "desc-event" );
        tableEvent.setWidget( 0, 1, eventContent );

        VerticalPanel eventNotes = new VerticalPanel();

        jug = new CombinedLabel( messages.jug() + " : " );
        eventNotes.add( jug );
        date = new CombinedLabel( messages.date() + " : " );
        eventNotes.add( date );
        time = new CombinedLabel( messages.time() + " : " );
        eventNotes.add( time );
        place = new CombinedLabel( messages.place() + " : " );
        eventNotes.add( place );
        seats = new CombinedLabel( messages.availableSeats() + " : " );
        eventNotes.add( seats );

        tableEvent.getCellFormatter().addStyleName( 0, 2, "notes-event" );
        tableEvent.setWidget( 0, 2, eventNotes );

        initWidget( eventPanel );

        tableEvent.addClickHandler( new ClickHandler()
        {

            @Override
            public void onClick( ClickEvent e )
            {
                eventController.showUpdateEvent( getEvent() );

            }
        } );
    }

    /**
     * Construct speakers string
     * 
     * @return Speakers
     */
    private String getSpeakers()
    {
        if ( event.getSpeakers() == null )
            return "";

        String speakers = "";
        for ( Iterator<String> iter = event.getSpeakers().iterator(); iter.hasNext(); )
        {
            speakers += iter.next();
            if ( iter.hasNext() )
            {
                speakers += ", ";
            }
        }
        return speakers;
    }

    public void setEvent( Event event )
    {
        this.event = event;

        event.addPropertyChangeListener( new PropertyChangeListener()
        {
            @Override
            public void propertyChange( PropertyChangeEvent evt )
            {
                updatePanel();
            }
        } );

        updatePanel();
    }

    private void updatePanel()
    {
        logoEvent.setUrl( event.getLogo() != null && event.getLogo().length() > 0 ? event.getLogo() : "images/logoEvent.png" );
        logoEvent.setTitle( event.getTitle() );
        title.setText( event.getTitle() );

        summary.setValue( event.getSummary() );
        speakers.setValue( getSpeakers() );
        jug.setValue( String.valueOf( event.getOrganizerId() ) );
        if ( event.getDateTime() != null )
        {
            date.setValue( dateFormatter.format( event.getDateTime() ) );
            time.setValue( timeFormatter.format( event.getDateTime() ) );
        }
        place.setValue( event.getPlace() );
        int participantsCount = event.getParticipantsId() != null ? event.getParticipantsId().size() : 0;
        int availableSeats = Math.max( event.getMax() - participantsCount, 0 );
        seats.setValue( String.valueOf( availableSeats ) + " / " + String.valueOf( event.getMax() ) );
    }

    public Event getEvent()
    {
        return event;
    }
}
